package mylist;

import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-04-27
 * Time: 20:19
 */
class MyLinkList {
    //创建节点
    static class ListNode {
        public int val;
        public ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    //创建链表
    public void createLink() {
        ListNode node1 = new ListNode(10);
        ListNode node2 = new ListNode(20);
        ListNode node3 = new ListNode(30);
        ListNode node4 = new ListNode(40);
        ListNode node5 = new ListNode(50);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }

    //打印链表
    public void display() {
        if( this.head == null) {
            System.out.println("链表为空");
            return ;
        }

        ListNode cur = this.head;
        while( cur != null) {
            System.out.print( cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //头插法
    public void addFirst(int data) {
        ListNode newnode = new ListNode(data);
        newnode.next = this.head;
        head = newnode;
    }
    //尾插法
    public void addLast(int data) {

        ListNode newnode = new ListNode(data);
        if( head == null ) {
            head = newnode;
            return ;
        }
        ListNode cur = this.head;
        while( cur.next != null) {
            cur = cur.next;
        }
        cur.next = newnode;
    }
    public ListNode findIndexSubOne(int index) {
        ListNode cur = this.head;
        int prev = 0;
        while( cur != null) {
            if( prev == index-1) {
                break;
            }
            prev++;
            cur = cur.next;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        //判断位置是否合法
        if( index < 0 || index > size() ) {
            throw new InsertIndexOutOfRangeException("要插入的位置不合法!");
        }
        //插入位置为 0
        if( index == 0) {
            addFirst(data);
            return true;
        }
        //插入位置为 链表尾
        if( index == size()) {
            addLast(data);
            return true;
        }
        //插入位置为中间位置
        ListNode newnode = new ListNode(data);
        ListNode cur = this.head;
        int prev = 0;
        //找到要插入位置的前一个位置
        cur = findIndexSubOne(index);
        //出循环 cur指向 要插入节点的前一个节点
        newnode.next = cur.next;
        cur.next = newnode;
        return true;
    }

    //查找是否包含关键字key是否在单链表当中
     public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur!=null) {
            if( cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        //判断链表为空
        if( head == null ) {
            System.out.println("链表为空,无法删除");
            return ;
        }
        //删除的是头结点
        if( head.val == key) {
            head = head.next;
            return ;
        }
        //删除中间节点
        ListNode cur = this.head;
        while(cur.next != null) {
            if( cur.next.val == key) {
                cur.next = cur.next.next;
                return ;
            }
            cur = cur.next;
        }
        System.out.println("要删除的值不存在");
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        //判断链表为空
        if( head == null ) {
            System.out.println("链表为空,无法删除");
            return ;
        }
        //删除的是头结点
        while( head.val == key) {
            head = head.next;
            if( head == null) {
                return ;
            }
        }
        ListNode cur = this.head.next;
        ListNode prev = this.head;
        //删除中间的值
        while( cur != null) {
            if( cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
                continue;
            }
            prev = cur;
            cur = cur.next;
        }
    }
    //得到单链表的长度
    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

//     //清空链表
     public void clear() {
        if( head == null) {
            System.out.println("链表已为空");
            return ;
        }
        head = null;
    }
}
public class Test01 {
    public static void main(String[] args) {
            MyLinkList link = new MyLinkList();
            link.createLink();
            link.addLast(1);
            link.addLast(3);
            link.addLast(3);
            link.addLast(3);
            link.addLast(1);

            link.display();
            link.removeAllKey(3);



    }
}
